import { ref } from 'vue'
// 猜你喜欢
import type { GuessItem } from '@/types/home'
import type { PageResult } from '@/types/global'
import { getGuessYouLike } from '@/services/home'

export const guessData = ref<PageResult<GuessItem>>() // 列表对象数据
export const guessList = ref<GuessItem[]>([]) // 列表数据
/** 获取首页猜你喜欢数据 */
export const getGuessData = async (page: number = 1, pageSize: number = 10) => {
  const res = await getGuessYouLike(page, pageSize)
  guessData.value = res.result
  guessList.value.push(...res.result.items)
}
const page = ref<number>(1) //当前页数
const pageSize = ref<number>(10) //页数大小
export const isFinite = ref<boolean>(false) // 数据是否全部加载完成
export const isTriggered = ref<boolean>(false) // 是否触发下拉刷新
/** 滚动视图滚动到底部触发事件 */
export const onScrollToLower = () => {
  if (!guessData.value?.page) return
  if (guessData.value.page >= guessData.value.pageSize) {
    isFinite.value = true
    uni.showToast({ title: '没有更多数据了', icon: 'none' })
  } else {
    page.value += 1
    getGuessData(page.value, pageSize.value)
  }
}
/** 滚动视图下拉刷新时需要重新获取的事件 */
export const onRefresh = async (promises: any) => {
  page.value = 1
  isFinite.value = false
  isTriggered.value = true
  Promise.all(promises).finally(() => {
    isTriggered.value = false
  })
}
